package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.AddressListBean;

import java.util.ArrayList;
import java.util.List;

import javax.crypto.Mac;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/10/19.
 */

public class ChooseAddressAdapter extends BaseAdapter {
	private List<AddressListBean.ListBean> list = new ArrayList<>();
	private Context mContext;
	private int mHeight;
	private boolean isManager;
	private int selectPosition = -1;
	private onMoreClickListener moreClickListener;
	public ChooseAddressAdapter(List<AddressListBean.ListBean> list, Context mContext , int mHeight) {
		this.list = list;
		this.mContext = mContext;
		this.mHeight = mHeight;
	}

	public void setOnMoreClickListner(onMoreClickListener moreClickListener) {
		this.moreClickListener = moreClickListener;
	}

	public void setSelectPosition(int selectPosition) {
		this.selectPosition = selectPosition;
	}

	public void setIsManager(boolean isManager) {
		this.isManager = isManager;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.choose_address_item_layout, null);
			holder.imageView = (ImageView) convertView.findViewById(R.id.iv_checkbox);
			holder.mMoreIv = (ImageView) convertView.findViewById(R.id.iv_more);
			holder.mAddressTv = (TextView) convertView.findViewById(R.id.address_tv);
			holder.mLinkManTv = (TextView) convertView.findViewById(R.id.link_man_tv);
			holder.mPhoneTv = (TextView) convertView.findViewById(R.id.phone_tv);
			holder.mDefaultIv = (ImageView) convertView.findViewById(R.id.iv_default);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (isManager) {
			holder.mMoreIv.setVisibility(View.VISIBLE);
			final View finalConvertView = convertView;
			holder.mMoreIv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					moreClickListener.onMore(finalConvertView, position);

				}
			});
			if ( !isManager && holder.imageView.isSelected()) {
				holder.imageView.setVisibility(View.VISIBLE);
			} else {
				holder.imageView.setVisibility(View.INVISIBLE);
			}
		} else {
			holder.mMoreIv.setVisibility(View.GONE);
		}
		if (selectPosition == -1) {
			if (!isManager) {
				if (list.get(position).isSelected()) {
					holder.imageView.setSelected(true);
					holder.imageView.setVisibility(View.VISIBLE);
				}
			} else {
				if (list.get(position).getIsDefault().equals("1")) {
					holder.imageView.setSelected(true);
					holder.imageView.setVisibility(View.VISIBLE);
				}
			}

		} else {
			if (selectPosition == position) {
				holder.imageView.setSelected(true);
			} else {
				holder.imageView.setSelected(false);
			}
		}
		if (isManager) {
			holder.imageView.setVisibility(View.GONE);
			if (list.get(position).getIsDefault().equals("1")) {
				holder.mDefaultIv.setVisibility(View.VISIBLE);
			} else {
				holder.mDefaultIv.setVisibility(View.INVISIBLE);
			}

		} else {
			holder.imageView.setVisibility(View.VISIBLE);
			holder.mDefaultIv.setVisibility(View.GONE);
		}

		if (list.size() > 0) {
			if (StringUtils.isNoneNullString(list.get(position).getAddress())) {
				holder.mAddressTv.setText(list.get(position).getAddress());
			}
			if (StringUtils.isNoneNullString(list.get(position).getMobilePhone())) {
				holder.mPhoneTv.setText(list.get(position).getMobilePhone());
			}
			if (StringUtils.isNoneNullString(list.get(position).getReceiveName())) {
				holder.mLinkManTv.setText(StringUtils.getResourceStr(mContext , R.string.link_man_tip , list.get(position).getReceiveName()));
			}
		}
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, mHeight / 7);
		convertView.setLayoutParams(lp);
		return convertView;
	}



	public class ViewHolder {
		private ImageView imageView;
		private ImageView mMoreIv;
		private TextView mAddressTv;
		private TextView mLinkManTv;
		private TextView mPhoneTv;
		private ImageView mDefaultIv;
	}

	public interface onMoreClickListener {
		void onMore(View view , int position);
	}


}
